﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using FhlowSharp.Core.FileDef;
using FhlowSharp.Core.Presenter;
using FhlowSharp.Gui.Properties;

namespace FhlowSharp.Gui
{
    public partial class MainView : Form, IFhlowUI
    {

        #region member

        private ApplicationConfig _config;

        #endregion member

        #region properties

        public FhlowSharpPresenter DataContext
        {
            get;
            set;
        }

        #endregion properties

        #region event

        public event EventHandler<InitPresenterEventArgs> InitPresenterEvent;

        #endregion event

        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        public MainView()
        {
            InitializeComponent();
            GetConfigs();
        }

        #endregion Constructors


        //public event EventHandler<UpdateFoldersEventArgs> UpdateFolder;
        //public event EventHandler SaveApplicationData;

        private ApplicationView _applicationView;
        

        private void OnSaveApplicationData()
        {
            //if(SaveApplicationData != null)
            //    SaveApplicationData(this, new EventArgs());
        }

        private void StartConfigWindows()
        {
            var changed = false;
            if (_config.FhlowFileStructure == null)
            {
                var optionsView = new OptionsView();
                optionsView.ShowDialog(this);
                _config.FhlowFileStructure = optionsView.FileStructure;
                changed = true;
            }

            if (changed)
            {
                TextWriter writer = new StreamWriter(Settings.Default.ApplicationConfigFilePath, false);
                var serializer = new XmlSerializer(typeof(ApplicationConfig), new[]{typeof(FhlowLeaf), typeof(FhlowComposite), typeof(FhlowComponent)});
                serializer.Serialize(writer, _config);
                writer.Close();
            }
        }

        private void GetConfigs()
        {

            try
            {
                using (TextReader reader = new StreamReader(Settings.Default.ApplicationConfigFilePath, false))
                {
                    var serializer = new XmlSerializer(typeof(ApplicationConfig), new[]{typeof(FhlowLeaf), typeof(FhlowComposite), typeof(FhlowComponent)});

                    _config = (ApplicationConfig)serializer.Deserialize(reader);

                    reader.Close();
                }

            }
            catch(Exception ex)
            {
                Debug.WriteLine("Can't load ApplicationConfig: " + ex.Message);
                _config = new ApplicationConfig();
            }
            StartConfigWindows();
        }

        void DataContext_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //if("ErrorState" == e.PropertyName)
            //{
            //    if(DataContext.ErrorState)
            //        ShowErrorMessage(DataContext.ErrorMessage);
            //}
            //else if("ShowApplView" == e.PropertyName)
            //{
            //    if (DataContext.ShowApplView)
            //    {
            //        _applicationView = new ApplicationView(DataContext.Applications);
            //        _applicationView.ShowDialog(this);
            //        OnSaveApplicationData();
            //    }
            //}
            //else if("FhlowGroupFolders" == e.PropertyName)
            //{
            //    foreach (var grpFolder in DataContext.FhlowGroupFolders)
            //    {
            //        var node = new TreeNode(grpFolder.Name);
            //        foreach (var unitFolder in grpFolder.Units)
            //        {
            //            var fileNode = new TreeNode(unitFolder.Name);

            //            foreach (var sourceFile in unitFolder.SourceFiles)
            //            {
            //                fileNode.Nodes.Add(Path.GetFileName(sourceFile.FilePath));
            //            }
            //            foreach (var file in unitFolder.Testbenches)
            //            {
            //                fileNode.Nodes.Add(Path.GetFileName(file.FilePath));
            //            }
            //            node.Nodes.Add(fileNode);
            //        }
            //        foreach (var pkgFolder in grpFolder.Packages)
            //        {
            //            var fnode = new TreeNode(pkgFolder.Name);
            //            fnode.Nodes.Add(Path.GetFileName(pkgFolder.Package.FilePath));
            //            node.Nodes.Add(fnode);
            //        }
            //        DirectoryList.Nodes.Add(node);
            //    }
                
            //}
        }



        //public FlowSharpPresenter DataContext
        //{
        //    get { return _dataContext; }
        //    set
        //    {
        //        _dataContext = value;
        //        DataContext.PropertyChanged += DataContext_PropertyChanged;
        //    }
        //}

        private void ShowErrorMessage(string message)
        {
            MessageBox.Show(this, message);
        }

        private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var openFile = new OpenFileDialog();
        }
      
        private void newProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            if(dialog.ShowDialog() == DialogResult.OK)
            {
                string folder = dialog.SelectedPath;
                //ScanFolders(folder);
                //UpdateFolder(this, new UpdateFoldersEventArgs(folder));
            }
        }

        private void optionsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            var dialog = new OptionsView(_config.FhlowFileStructure);
            dialog.ShowDialog(this);
        }
    }
}
